//
//  MainViewController.swift
//  SinaWeiBo20
//
//  Created by tarena on 16/3/18.
//  Copyright © 2016年 tarena. All rights reserved.
//

import UIKit


class MainViewController: UITabBarController {
    
    //添加的按钮点击事件
    @objc private func composeDidClick() {
        print(__FUNCTION__)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        
        let mainTabBar = MainTabBar()
        setValue(mainTabBar, forKey: "tabBar")
        //添加子视图控制器
        addChildViewControllers()
        
        //给按钮添加点击时间
        mainTabBar.composeBtn.addTarget(self, action: "composeDidClick", forControlEvents: .TouchUpInside)
        
    }
    
    private func addChildViewControllers() {
        
        addChildViewController(HomeTableViewController(), title: "首页", imageName: "tabbar_home",index: 0)
        addChildViewController(MessageTableViewController(), title: "消息", imageName: "tabbar_message_center",index: 1)
        addChildViewController(DiscoverTableViewController(), title: "发现", imageName: "tabbar_discover",index: 2)
        addChildViewController(ProfileTableViewController(), title: "我", imageName: "tabbar_profile",index: 3)
    }
    
    private func addChildViewController(vc : UIViewController, title : String, imageName : String, index: Int){
        vc.title = title;
        vc.tabBarItem.image = UIImage(named: imageName)
        vc.tabBarItem.tag = index
        addChildViewController(UINavigationController(rootViewController: vc))

    }
    
    
    /**
    *1.要知道用户点击的是 哪一个 item
    *2.需要知道点击的是一个 tabarButton
    *3.需要tabBarButton 中的imageView 取出来 实现动画效果
    */
    
    //tabBar的协议方法
    private var lastSelectedItem: UITabBarItem = HomeTableViewController().tabBarItem
    override func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
//        print(item.tag)
        var index :Int = 0
        for button in tabBar.subviews {
            if button.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                if item.tag == index && lastSelectedItem.tag != index {
                    for imageView in button.subviews {
                        if imageView.isKindOfClass(NSClassFromString("UITabBarSwappableImageView")!){
                            imageView.transform = CGAffineTransformMakeScale(0.5, 0.5)
                            UIView.animateWithDuration(0.27, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 2, options: [], animations: { () -> Void in
                                imageView.transform = CGAffineTransformIdentity
                                }, completion: { (_) -> Void in
//                                    print("OK")
                            })
                    
                        }
                    
                    }
                }
                index++
            }
        }
        lastSelectedItem = item
    
    }
    
    override func tabBar(tabBar: UITabBar, didBeginCustomizingItems items: [UITabBarItem]) {
        print("tabBar:\(tabBarItem)");
    }
}
